package com.karaku.karaku_collector.collector;

import com.karaku.karaku_collector.UmengEventManager;
import com.karaku.karaku_collector.agora.CollectorRtcEventHandler;
import com.karaku.karaku_collector.util.LogUtil;
import com.karaku.karaku_collector.util.ThreadUtil;

import io.agora.rtc.IRtcEngineEventHandler;

/**
 * 采集状态管理
 */

public class CollectorStateManager {

    public interface Listener {
        void onStateChanged(int state);
    }

    public static final int STATE_STOPPED = -1;
    public static final int STATE_NORMAL = 0;
    public static final int STATE_RETRY_CAMERA_ERROR = 1;
    public static final int STATE_RETRY_SDK_LAUNCH = 2;
    public static final int STATE_RETRY_SDK_CONNECT = 3;
    public static final int STATE_RETRY_SDK_ERROR = 4;
    public static final int STATE_RETRY_CHANNEL_KEY_EXPIRED = 5;
    public static final int STATE_RETRY_GET_CHANNEL_KEY = 6;
    public static final int STATE_RETRY_UPLOAD_ROOM_INFO = 7;
    public static final int STATE_RETRY_TIMEOUT = 8;
    public static final int STATE_RETRY_CRASH = 9;

    private static CollectorStateManager instance;

    public static CollectorStateManager getInstance() {
        if (instance == null) {
            instance = new CollectorStateManager();
        }
        return instance;
    }

    private int state = STATE_STOPPED;
    private String stateDescription;
    private CollectorStat stat;
    private Listener listener;

    private IRtcEngineEventHandler eventHandler = new IRtcEngineEventHandler() {
        @Override
        public void onError(int err) {
            setState(STATE_RETRY_SDK_ERROR, getAgoraErrorDesc(err));
        }

        @Override
        public void onRequestChannelKey() {
            setState(STATE_RETRY_CHANNEL_KEY_EXPIRED, "");
        }

        @Override
        public void onConnectionInterrupted() {
            setState(STATE_RETRY_SDK_CONNECT, null);
        }

        @Override
        public void onRejoinChannelSuccess(String channel, int uid, int elapsed) {
            if (CollectorManager.getInstance().getState() == CollectorManager.STATE_STARTED) {
                setState(STATE_NORMAL, null);
            }
        }
    };

    private CollectorStateManager() {
        CollectorRtcEventHandler.getGlobalHandler().addSubHandler(eventHandler);
    }

    public Listener getListener() {
        return listener;
    }

    public void setListener(Listener listener) {
        this.listener = listener;
    }

    public int getState() {
        return state;
    }

    public void setState(int state, String description) {
        LogUtil.d("STATE", "state:" + state + " desc:" + description);
        stateDescription = description;
        if (this.state == state) {
            return;
        }
        UmengEventManager.onCollectorErrorEvent(state, description);
        this.state = state;
        ThreadUtil.runOnMainThread(new Runnable() {
            @Override
            public void run() {
                if (listener != null) {
                    listener.onStateChanged(CollectorStateManager.this.state);
                }
            }
        });
    }

    public CollectorStat getStat() {
        if (stat == null) {
            stat = new CollectorStat();
        }
        if (state == STATE_NORMAL) {
            stat.state = 0;
            stat.reason = 0;
        } else {
            stat.state = 1;
            stat.reason = state;
        }
        stat.desc = getStateDescription();
        return stat;
    }

    public String getStateDescription() {
        return stateDescription == null ? "" : stateDescription;
    }

    public static String getStateName(int state) {
        switch (state) {
            case STATE_STOPPED:
                return "未启动";
            case STATE_NORMAL:
                return "正常";
            case STATE_RETRY_CAMERA_ERROR:
                return "摄像头启动失败";
            case STATE_RETRY_SDK_LAUNCH:
                return "声网启动失败";
            case STATE_RETRY_SDK_CONNECT:
                return "声网连接失败";
            case STATE_RETRY_SDK_ERROR:
                return "声网内部错误";
            case STATE_RETRY_CHANNEL_KEY_EXPIRED:
                return "ChannelKey失效";
            case STATE_RETRY_GET_CHANNEL_KEY:
                return "请求动态Key失败";
            case STATE_RETRY_UPLOAD_ROOM_INFO:
                return "上传房间信息失败";
            case STATE_RETRY_TIMEOUT:
                return "重试超时";
            case STATE_RETRY_CRASH:
                return "AppCrash";
        }
        return "未知状态";
    }

    public static String getAgoraErrorDesc(int error) {
        switch (error) {
            case 101: return "无效的 App ID";
            case 102: return "无效的频道名";
            case 105: return "查找频道失败，意味着服务器主动拒绝了请求";
            case 107: return "加入频道失败，意味着媒体服务器主动拒绝了请求";
            case 1001: return "加载媒体引擎失败";
            case 1002: return "打开本地音视频设备、启动通话失败";
            case 1003: return "打开本地摄像头失败";
            default: return "未知错误";
        }
    }

    public static class CollectorStat {
        public int state;
        public int reason;
        public String desc;
    }
}
